package com.tsd.system.vo;

import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.FieldUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: GlobalSetting
 * @Description: TODO
 * @Author: Hillpool
 * @Date: 2022/10/17 17:37
 * @Version: 1.0
 */
public class GlobalSetting {

    public static final String SETTING_CODE_GLOBAL_SETTING = "global_setting";

    public static final String TYPE_CLIENT = "client";
    public static final String TYPE_SUPPLIER = "supplier";

    public static final String DISPLAY_CODE = "code";
    public static final String DISPLAY_NAME = "name";
    public static final String DISPLAY_SHORT_NAME = "short_name";
    //启用值
    public static final int STATE_ENABLE = 1;
    //关闭值
    public static final int STATE_DISABLE = 0;

    // 是否启用数据范围授权：0-关闭、1-开启
    private int enable_data_auth = STATE_DISABLE;

    // 是否启用数据字段授权：0-关闭、1-开启
    private int enable_data_auth_field = STATE_DISABLE;

    // 往来单位显示：code-编码、name-名称、short_name-简称
    private String related_party_display = DISPLAY_NAME;

    /**
     * 登录后跳转的url
     */
    private String url_after_logined;

    private List<String> client_auth_view_field = new ArrayList<>();

    private List<ColumnSetting> client_auth_view_column_list = new ArrayList<>();

    public int getEnable_data_auth() {
        return enable_data_auth;
    }

    public void setEnable_data_auth(int enable_data_auth) {
        this.enable_data_auth = enable_data_auth;
    }

    public int getEnable_data_auth_field() {
        return enable_data_auth_field;
    }

    public void setEnable_data_auth_field(int enable_data_auth_field) {
        this.enable_data_auth_field = enable_data_auth_field;
    }

    public String getRelated_party_display() {
        return related_party_display;
    }

    public void setRelated_party_display(String related_party_display) {
        this.related_party_display = related_party_display;
    }

    public String getUrl_after_logined() {
        return url_after_logined;
    }

    public void setUrl_after_logined(String url_after_logined) {
        this.url_after_logined = url_after_logined;
    }

    public List<String> getClient_auth_view_field() {
        return client_auth_view_field;
    }

    public void setClient_auth_view_field(List<String> client_auth_view_field) {
        this.client_auth_view_field = client_auth_view_field;
    }

    public List<ColumnSetting> getClient_auth_view_column_list() {
        return client_auth_view_column_list;
    }

    public void setClient_auth_view_column_list(List<ColumnSetting> client_auth_view_column_list) {
        this.client_auth_view_column_list = client_auth_view_column_list;
    }

    public void procRelatedPartyDisplayBase(Object object) {
        if (DISPLAY_CODE.equals(this.getRelated_party_display())) {
            Object value = FieldUtils.getFieldValue(object, "code");
            FieldUtils.setFieldValue(object, "display", value, true);
        } else if (DISPLAY_NAME.equals(this.getRelated_party_display())) {
            Object value = FieldUtils.getFieldValue(object, "name");
            FieldUtils.setFieldValue(object, "display", value, true);
        } else if (DISPLAY_SHORT_NAME.equals(this.getRelated_party_display())) {
            Object value = FieldUtils.getFieldValue(object, "short_name");
            FieldUtils.setFieldValue(object, "display", value, true);
        }
        FieldUtils.clearField(object, "code", "name", "short_name");
    }

    public void procRelatedPartyDisplay(Object object) {
        if (DISPLAY_CODE.equals(this.getRelated_party_display())) {
            Object value = FieldUtils.getFieldValue(object, "related_party_code");
            FieldUtils.setFieldValue(object, "related_party_display", value, true);
        } else if (DISPLAY_NAME.equals(this.getRelated_party_display())) {
            Object value = FieldUtils.getFieldValue(object, "related_party_name");
            FieldUtils.setFieldValue(object, "related_party_display", value, true);
        } else if (DISPLAY_SHORT_NAME.equals(this.getRelated_party_display())) {
            Object value = FieldUtils.getFieldValue(object, "related_party_short_name");
            FieldUtils.setFieldValue(object, "related_party_display", value, true);
        }
        FieldUtils.clearField(object, "related_party_code", "related_party_name", "related_party_short_name");
    }

    public void procDisplay(Object object, String type) {
        if (TYPE_CLIENT.equals(type)) {
            if (DISPLAY_CODE.equals(this.getRelated_party_display())) {
                Object value = FieldUtils.getFieldValue(object, "client_code");
                if (HlpUtils.isEmpty(value)) {
                    value = FieldUtils.getFieldValue(object, "client_code_join");
                }
                if (HlpUtils.isEmpty(value)) {
                    value = FieldUtils.getFieldValue(object, "order_client_code");
                }
                FieldUtils.setFieldValue(object, "client_display", value, true);
            } else if (DISPLAY_NAME.equals(this.getRelated_party_display())) {
                Object value = FieldUtils.getFieldValue(object, "client_name");
                if (HlpUtils.isEmpty(value)) {
                    value = FieldUtils.getFieldValue(object, "order_client_name");
                }
                FieldUtils.setFieldValue(object, "client_display", value, true);
            } else if (DISPLAY_SHORT_NAME.equals(this.getRelated_party_display())) {
                Object value = FieldUtils.getFieldValue(object, "client_short_name");
                if (HlpUtils.isEmpty(value)) {
                    value = FieldUtils.getFieldValue(object, "order_client_short_name");
                }
                FieldUtils.setFieldValue(object, "client_display", value, true);
            }
            if (FieldUtils.isFieldExist(object, "client_code_join")) {
                FieldUtils.clearField(object, "client_code_join", "client_name", "client_short_name");
            } else {
                FieldUtils.clearField(object, "client_code", "client_name", "client_short_name");
            }
            FieldUtils.clearField(object, "order_client_code", "order_client_name", "order_client_short_name");
        } else if (TYPE_SUPPLIER.equals(type)) {
            if (DISPLAY_CODE.equals(this.getRelated_party_display())) {
                Object value = FieldUtils.getFieldValue(object, "supplier_code");
                FieldUtils.setFieldValue(object, "supplier_display", value, true);
            } else if (DISPLAY_NAME.equals(this.getRelated_party_display())) {
                Object value = FieldUtils.getFieldValue(object, "supplier_name");
                if (HlpUtils.isEmpty(value)) {
                    value = FieldUtils.getFieldValue(object, "supplier_name_join");
                }
                FieldUtils.setFieldValue(object, "supplier_display", value, true);
            } else if (DISPLAY_SHORT_NAME.equals(this.getRelated_party_display())) {
                Object value = FieldUtils.getFieldValue(object, "supplier_short_name");
                FieldUtils.setFieldValue(object, "supplier_display", value, true);
            }
            FieldUtils.clearField(object, "supplier_code", "supplier_name", "supplier_name_join", "supplier_short_name");
        }
    }

    public void procDisplay4Assets(Object object) {
        if (DISPLAY_CODE.equals(this.getRelated_party_display())) {
            Object value = FieldUtils.getFieldValue(object, "client_code");
            Object ownerValue = FieldUtils.getFieldValue(object, "owner_code");
            FieldUtils.setFieldValue(object, "client_display", value, true);
            FieldUtils.setFieldValue(object, "owner_display", ownerValue, true);
        } else if (DISPLAY_NAME.equals(this.getRelated_party_display())) {
            Object value = FieldUtils.getFieldValue(object, "client_name");
            Object ownerValue = FieldUtils.getFieldValue(object, "owner_name");
            FieldUtils.setFieldValue(object, "client_display", value, true);
            FieldUtils.setFieldValue(object, "owner_display", ownerValue, true);
        } else if (DISPLAY_SHORT_NAME.equals(this.getRelated_party_display())) {
            Object value = FieldUtils.getFieldValue(object, "client_short_name");
            Object ownerValue = FieldUtils.getFieldValue(object, "owner_short_name");
            FieldUtils.setFieldValue(object, "client_display", value, true);
            FieldUtils.setFieldValue(object, "owner_display", ownerValue, true);
        }
        FieldUtils.clearField(object, "client_code", "client_name", "client_short_name");
        FieldUtils.clearField(object, "owner_code", "owner_name", "owner_short_name");
    }
}
